#ifndef __PICTURES__
#define __PICTURES__
#include "picfile.h"

struct Header {
  unsigned VerSize, HorSize, PixType;//, ComSize;
};

class Picture : public PicFile {
public:		// Data fields
   Header H;
   int ifOpened;

protected:
   void ClearH() {
      ifOpened=0;
      H.HorSize=0;
      H.VerSize=0;
      H.PixType=0;
   }
   void DefineH() {
      ifOpened=1;
      H.HorSize=(unsigned)GetHorSize();
      H.VerSize=(unsigned)GetVerSize();
      H.PixType=GetPixelBitNumber();
   }
public:		// Input/Output Procedures
  Picture() : PicFile(), ifOpened(0) {}
  Picture(const char* fname) : PicFile() {
     if( PicFile::OpenFile(fname)==frcOk ) DefineH();
     else ClearH();
  }

  Picture(const char* fname,
     unsigned VerSize,unsigned HorSize,unsigned PixType) : PicFile() {
     if( PicFile::OpenFile(fname, VerSize, HorSize, PixType)==frcOk ) DefineH();
     else ClearH();
  }

  Picture(const char* fname, Header &AH) : PicFile() {
     if( PicFile::OpenFile(fname, AH.VerSize, AH.HorSize, AH.PixType)==frcOk ) DefineH();
     else ClearH();
  }

  ~Picture() { ClearH(); };

  int Open(const char* fname) {
     if( PicFile::OpenFile(fname)==frcOk ) DefineH();
     else ClearH();
     return (ifOpened) ? 0 : 1;
  }

  int Open(const char* fname,
     unsigned VerSize,unsigned HorSize,unsigned PixType) {
     if( PicFile::OpenFile(fname, VerSize, HorSize, PixType)==frcOk ) DefineH();
     else ClearH();
     return (ifOpened) ? 0 : 1;
  }

  int Open(const char* fname,Header &AH)
      { return Open(fname,AH.VerSize,AH.HorSize,AH.PixType); }

  void Close() { CloseFile(); ClearH(); }

  int ReadLine(unsigned line,void* abuf) {
     return ( PicFile::ReadString(line, abuf)==frcOk ) ? 0 : -1;
  }

  int WriteLine(unsigned line,void* abuf) {
     return ( PicFile::WriteString(line, abuf)==frcOk ) ? 0 : -1;
  }

  // Comment functions
  int AddField ( char *FieldName, char *FieldText ) {
     return (PicFile::AddComment(FieldName, FieldText)==frcOk) ? 0 :-1;
  }

  char *GetField (char *FieldName ) {
     char* s=new char[GetCommentLength(FieldName)+2];
     if( s ) {
        if( GetComment(FieldName, s)!=frcOk ) { delete s; s=0; }
     }
     return s;
  }

  int DelField ( char *FieldName ) {
     return ( DelComment(FieldName)==frcOk ) ? 0 : 1;
  }

  int Opened() { return ifOpened; }
};

#endif
